package cn.xpg.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.xpg.entity.CustomerType;
import cn.xpg.service.CustomerTypeService;
import cn.xpg.tools.PageSupport;

@Controller
public class CustomerTypeController {

	@Resource(name = "customerTypeService")
	private CustomerTypeService customerTypeService;

	// 分页查询客户类型
	@RequestMapping("/getCustomerTypeList.do")
	public String getCustomerTypeList(HttpServletRequest request) {
		String pageIndex = request.getParameter("pageIndex");
		String customertype_name = request.getParameter("customertype_name");
		int pageSize = 3;
		int currentPageNo = 1;

		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		int totalCount = 0;
		totalCount = customerTypeService.getCustomerTypeCount(customertype_name);
		System.out.println("查询客户类型信息！！！！！！！！！！————>getCustomerTypeList");
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);

		int totalPageCount = pages.getTotalPageCount();
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}
        
		List<CustomerType> customerTypeList = null;
		customerTypeList = customerTypeService.getCustomerTypeList(customertype_name, (currentPageNo - 1) * pageSize,
				pageSize);

		request.setAttribute("customerTypeList", customerTypeList);
		request.setAttribute("totalPageCount", totalPageCount);
		request.setAttribute("pages", pages);
		request.setAttribute("customertype_name", customertype_name);
		return "/jsp/base/customer_classify";
	}

	// 新增客户类型
	@RequestMapping("addCustomerType.do")
	public String addCustomer(@Valid CustomerType customerType) {
		System.out.println("新增客户类型信息！！！！！！！！！！————>addCustomerType");
		if (customerTypeService.addCustomerType(customerType) > 0) {
			return "redirect:/getCustomerTypeList.do";
		} else {
			return "/jsp/base/customer_classify";
		}
	}

	// 获取客户类型Id
	@RequestMapping("getCustomerTypeId.do")
	public String getCustomerId(HttpServletRequest request) {
		System.out.println("获取客户类型Id！！！！！！！！！！————>getCustomerId");
		int customertype_id = Integer.parseInt(request.getParameter("customertype_id"));
		CustomerType customerType = customerTypeService.getCustomerTypeId(customertype_id);
		request.setAttribute("customerType", customerType);
		return "/jsp/base/customer_classify";
	}

	// 修改客户类型
	@RequestMapping("updateCustomerType.do")
	public String updateCustomer(@Valid CustomerType customerType, HttpServletRequest request) {
		System.out.println("修改客户类型信息！！！！！！！！！！————>updateCustomerType");
		if (customerTypeService.updateCustomerType(customerType) > 0) {
			System.out.println("修改成功！！！！！！");
			return "redirect:/getCustomerTypeList.do";
		} else {
			System.out.println("修改失败！！！！！！");
			request.setAttribute("customerType", customerType);
			return "/jsp/base/customer_classify";
		}
	}

	// 删除客户类型
	@RequestMapping("deleteCustomerType.do")
	public String deleteCustomer(HttpServletRequest request) {
		System.out.println("删除客户类型信息！！！！！！！！！！————>deleteCustomerType");
		int customertype_id = Integer.valueOf(request.getParameter("customertype_id"));
		if (customerTypeService.deleteCustomerType(customertype_id) > 0) {
			System.out.println("删除成功！！！！！！");
		} else {
			System.out.println("删除失败！！！！！！");
		}
		return "redirect:/getCustomerTypeList.do";
	}
}
